$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var potentialClientDatagrid, potentialClientDialog, potentialClientForm, followupDialog, followupForm,transferBillDialog,
        transferBillForm,potentialClientSearchBtn;
    potentialClientDatagrid = $("#potentialClient_datagrid");
    potentialClientDialog = $("#potentialClient_dialog");
    potentialClientForm = $("#potentialClient_form");
    followupDialog = $("#followupBill_dialog");
    followupForm = $("#followupBill_form");
    transferBillDialog = $("#transferBill_dialog");
    transferBillForm = $("#transferBill_form");
    potentialClientSearchBtn=$("#searchBtn");
    /*
     * 初始化数据表格
     */
    potentialClientDatagrid.datagrid({
        url: "/potentialClient/list",
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#potentialClient_datagrid_tb',
        columns: [
            [
                {field: 'clientsn', width: 10, align: 'center', title: '客户编号'},
                {field: 'username', width: 10, align: 'center', title: '用户名'},
                {
                    field: 'clienttype',
                    width: 10,
                    align: 'center',
                    title: '客户类型',
                    formatter: function (value, row, index) {
                        return value == 1 ? "公司法人" : "个人";
                    }
                },
                {
                    field: 'status', width: 10, align: 'center', title: '状态', formatter: function (value, row, index) {
                    if (value == 1) {
                        return "正式客户";
                    } else if (value == 0) {
                        return "潜在客户";
                    } else {
                        return "无效客户";
                    }
                }
                },
                {field: 'realname', width: 10, align: 'center', title: '真实姓名'},
                {field: 'idcardtype', width: 10, align: 'center', title: '证件类型'},
                {field: 'idcardnumber', width: 10, align: 'center', title: '证件号码'},
                {
                    field: 'gender', width: 10, align: 'center', title: '性别', formatter: function (value, row, index) {
                    if (value == 1) {
                        return "男";
                    } else if (value == 0) {
                        return "女";
                    } else {
                        return "未知";
                    }
                }
                },
                {field: 'phonenumber', width: 10, align: 'center', title: '电话号码'},
                {field: 'email', width: 10, align: 'center', title: '邮箱'},
                {field: 'address', width: 10, align: 'center', title: '地址'},
                {
                    field: 'applicant',
                    width: 10,
                    align: 'center',
                    title: '是否投保人',
                    formatter: function (value, row, index) {
                        if (value == -1) {
                            return "被投保人";
                        } else if (value == 0) {
                            return "投保人";
                        } else {
                            return "车主";
                        }
                    }
                },
                {field: 'inputtime', width: 10, align: 'center', title: '录入时间'},
                {field: 'origin', width: 10, align: 'center', title: '客户来源'},
                {
                    field: 'clientlevel',
                    width: 10,
                    align: 'center',
                    title: '客户等级',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "高级";
                        } else if (value == 0) {
                            return "中等";
                        } else {
                            return "普通";
                        }
                    }
                },
                {field: 'remark', width: 10, align: 'center', title: '备注'},
                {
                    field: 'inputor',
                    width: 10,
                    align: 'center',
                    title: '录入人',
                    formatter: function (value, row, index) {
                        return value ? value.realname : "无";
                    }
                },
                {
                    field: 'manager',
                    width: 10,
                    align: 'center',
                    title: '负责人',
                    formatter: function (value, row, index) {
                        return value ? value.realname : "无";
                    }
                },
                {
                    field: 'sharer', width: 10, align: 'center', title: '共享人', formatter: function (value, row, index) {
                    return value ? value.realname : "无";
                }
                }
            ]
        ]
    });
    /*
     * 初始化新增/编辑对话框
     */
    potentialClientDialog.dialog({
        width: 550,
        height: 320,
        closed: true,
        buttons: '#potentialClient_dialog_bt'
    });
    followupDialog.dialog({
        width: 300,
        height: 300,
        closed: true,
        buttons: '#followupBill_dialog_bt'
    });
    transferBillDialog.dialog({
        width: 300,
        height: 300,
        closed: true,
        buttons: '#transferBill_dialog_bt'
    });
    potentialClientSearchBtn.textbox({
        width:230,
        label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
            var keyword = $(this).val();
            potentialClientDatagrid.datagrid("load",{
                keyword:keyword
            });
        }
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        //客户移交
        transfer: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                transferBillForm.form("clear");
                transferBillDialog.dialog("setTitle", "移交");
                transferBillDialog.dialog("open");
                rowData['client.realname'] = rowData.realname;
                rowData['client.id'] = rowData.id;
                rowData['transfer.realname'] = rowData.manager.realname;
                rowData['transfer.id'] = rowData.manager.id;
                transferBillForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要移交的客户!", "warining");
            }
            /*if (rowData) {
                /!*transferBillForm.form("clear");
                transferBillDialog.dialog("setTitle", "编辑");
                transferBillDialog.dialog("open");
                rowData['potentialClient.realname']=rowData.realname;
                rowData['potentialClient.id']=rowData.id;
                rowData['transfer.realname']=rowData.manager.realname;
                rowData['transfer.id']=rowData.manager.id;
                transferBillForm.form("load", rowData);*!/
            } else {
                $.messager.alert("温馨提示", "请选择需要移交的客户!", "warining");
            }*/
        },
        saveTransferBill: function () {
        var url;
        var idVal = $("[name='id']").val();
        if (idVal) {
            url = "/transferBill/update"
        } else {
            url = "/transferBill/save";
        }
        transferBillForm.form("submit", {
            url: url,
            success: function (data) {
                data = $.parseJSON(data);
                if (data.success) {
                    $.messager.alert("温馨提示", data.msg, "info", function () {
                        transferBillDialog.dialog("close");
                        potentialClientDatagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert("温馨提示", data.msg, "error");
                }
            }
        });
    },
    cancelTransferBill: function () {
        transferBillDialog.dialog("close");
    },
        //客户跟进
        followup: function () {
            //把缓存中所有的数据重新加载allPermission的数据表格中
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                followupForm.form("clear");
                followupDialog.dialog("setTitle", "新增");
                followupDialog.dialog("open");
                rowData['potentialClient.id'] = rowData.id;
                followupForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要跟进的客户!", "warining");
            }
        },
        saveBill: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/followupBill/update"
            } else {
                url = "/followupBill/save";
            }
            followupForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            followupDialog.dialog("close");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancelBill: function () {
            followupDialog.dialog("close");
        },
        //放入资源池
        putPool: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要将此客户放入资源池吗？", function (yes) {
                    if (yes) {
                        $.get("/client/putPool?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    potentialClientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //转正
        formal: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要将此客户转为正式客户吗？", function (yes) {
                    if (yes) {
                        $.get("/client/formal?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    potentialClientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //无效客户
        invalid: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要无效此客户吗？", function (yes) {
                    if (yes) {
                        $.get("/client/invalid?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    potentialClientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要操作的数据!", "warining");
            }
        },
        //增删改,
        add: function () {
            //把缓存中所有的数据重新加载allPermission的数据表格中
            potentialClientForm.form("clear");
            potentialClientDialog.dialog("setTitle", "新增");
            potentialClientDialog.dialog("open");
        },
        edit: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                potentialClientForm.form("clear");
                potentialClientDialog.dialog("setTitle", "编辑");
                potentialClientDialog.dialog("open");
                potentialClientForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要编辑的数据!", "warining");
            }
        },
        del: function () {
            var rowData = potentialClientDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要删除选中数据吗？", function (yes) {
                    if (yes) {
                        $.get("/client/delete?clientId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    potentialClientDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要删除的数据!", "warining");
            }
        },
        //刷新,确认,取消
        reload: function () {
            potentialClientDatagrid.datagrid("reload");
        },
        save: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/client/update"
            } else {
                url = "/client/save";
            }
            potentialClientForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            potentialClientDialog.dialog("close");
                            potentialClientDatagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancel: function () {
            potentialClientDialog.dialog("close");
        },
    }
});
